from pydantic import BaseModel,EmailStr
from sqlmodel import SQLModel,Field,Relationship
from typing import List, Dict, Any
from datetime import datetime

class UserDB(BaseModel):
    username:str
    email:str
    hashed_password:str

class UserConfig(SQLModel):
    id: int
    username: str
    email: EmailStr
    money: int
    chats: List["ChatDB"] = []  # 这里会包含完整的对话和消息数据

class UserReg(BaseModel):
    username:str
    email:str
    code:str
    password:str

    
class User(SQLModel, table=True):
    __tablename__ = "user"  # 添加表名定义
    
    id: int = Field(default=None, primary_key=True)
    username: str = Field(unique=True)
    email: str = Field(unique=True)
    hashed_password: str
    money:int
    
    # 使用字符串类型注解解决循环导入
    chats: List["ChatDB"] = Relationship(back_populates="user")

class EmailRequest(BaseModel):
    email: EmailStr

class Email(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True)
    sender : str
    user : str
    port: int
    passwd: str

class TokenData(BaseModel):
    username: str | None = None

#创建令牌模型
class Token(BaseModel):
    access_token: str
    token_type: str

# 在文件末尾导入，解决循环导入问题
from model.chat_model import ChatDB

# 新增前端响应模型
class ChatMessageResponse(BaseModel):
    id: str
    isuser: bool
    content: str

class ChatItemResponse(BaseModel):
    id: str
    username: str
    title: str
    createdAt: datetime
    chatmessages: List[ChatMessageResponse] = []

class UserResponse(BaseModel):
    id: int
    username: str
    email: str
    money: int
    chats: List[ChatItemResponse] = []

class LoginResponse(BaseModel):
    token: Token
    user: UserResponse